home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Moscow ML 1.42 / src / lex / Mainlex.sml < prev    next >
Encoding:
Text File  |  1997-08-18  |  1.7 KB  |  54 lines  |  [TEXT/R*ch]

  1. (* The lexer generator. Command-line parsing. *)
  2.  
  3. open Lexing Parsing Miscsys;
  4. open Syntax Scanner Grammar Lexgen Output;
  5.  
  6. (* Lexer of stream *)
  7.  
  8. fun createLexerStream (is : BasicIO.instream) =
  9.   Lexing.createLexer (fn buff => fn n => Nonstdio.buff_input is buff 0 n)
  10. ;
  11.  
  12. fun main () =
  13.   let val () =
  14.         if Vector.length command_line <> 2 then
  15.           (output(std_err, "Usage: mosmllex <input file>\n");
  16.            flush_out std_err;
  17.            BasicIO.exit 2)
  18.         else ()
  19.       val source_name = Vector.sub(command_line, 1)
  20.       val dest_name =
  21.         if Filename.check_suffix source_name ".lex" then
  22.           Filename.chop_suffix source_name ".lex" ^ ".sml"
  23.         else
  24.           source_name ^ ".sml" 
  25.       val () = (is := open_in_bin source_name)
  26.       val () = (os := open_out dest_name)
  27.       val lexbuf =
  28.         createLexerStream (!is)
  29.       val def as Lexdef(header,_) =
  30.         lexer_definition Scanner.main lexbuf
  31.         handle
  32.           ParseError x =>
  33.             (output(std_err, "Syntax error around char ");
  34.              output(std_err, makestring (getLexemeStart lexbuf));
  35.              output(std_err, ".\n"); flush_out std_err;
  36.              BasicIO.exit 2)
  37.         | Scan_aux.Lexical_error s =>
  38.             (output(std_err, "Lexical error around char ");
  39.              output(std_err, makestring (getLexemeStart lexbuf));
  40.              output(std_err, ": ");
  41.              output(std_err, s);
  42.              output(std_err, ".\n"); flush_out std_err;
  43.              BasicIO.exit 2)
  44.       val dfa as (init, states, acts) = make_dfa def 
  45.   in
  46.     output_lexdef header dfa;
  47.     close_in (!is);
  48.     close_out (!os);
  49.     BasicIO.exit 0
  50.   end
  51. ;
  52.  
  53. val () = Printexc.f main ();
  54.